package datamining.code.dao;

import java.nio.charset.CodingErrorAction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import datamining.code.connection.OracleConnection;
import datamining.code.model.Emprestimo;

public class EmprestimoDAO {
	Connection connection = OracleConnection.getConnection();
	
	public ArrayList<Object> retrieve() throws SQLException {
		ArrayList<Object> result = new ArrayList<Object>();
		String sql = "SELECT codigo, codpublicacao, tipo, dtemprestimo, dtdevprevista, dtdevefetiva, dtreserva, categoria FROM emprestimo_2 order by codigo";
		PreparedStatement stmt = connection.prepareStatement(sql);
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
			Emprestimo emp = new Emprestimo();
			emp.setCodigo(rs.getInt("codigo"));
			emp.setCodpublicacao(rs.getInt("codpublicacao"));
			emp.setDtEmprestimo(rs.getString("dtemprestimo"));
			emp.setDtDevPrevista(rs.getString("dtdevprevista"));
			emp.setDtDevEfetiva(rs.getString("dtdevefetiva"));
			emp.setDtReserva(rs.getString("dtreserva"));
			emp.setCategoria(rs.getString("categoria"));
			emp.setTipo(rs.getString("tipo"));
			result.add(emp);
		}
		System.out.println(result.size());
		if (result.size()>0) {
			return result;
		}
			return null;
	}
	
	public String retrieveSubject(int codpublicacao) {
		String sql = "SELECT assunto FROM dados WHERE idpublicacao=?";
		PreparedStatement stmt;
		String assunto="";
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setInt(1, codpublicacao);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				assunto= rs.getString("assunto");
//				if(assunto.equals(""))
//					System.out.println(codpublicacao);
			}
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return assunto;
	}

}
